package cn.swh.guguanjia.controller;

import cn.swh.guguanjia.entity.Result;
import cn.swh.guguanjia.entity.SysArea;
import cn.swh.guguanjia.service.SysAreaService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/mapper/area")
public class SysAreaController {


    @Autowired
    SysAreaService service;


    @RequestMapping("")
    public ModelAndView area() {
        return new ModelAndView("/area/area.html");
    }


    @RequestMapping({"/toUpdate", "/update.html"})
    public ModelAndView toUpdate() {
        return new ModelAndView("/area/update.html");
    }

//    @RequestMapping(value = "doUpdate", method = RequestMethod.POST)
//    public Result doUpdate(@RequestBody SysArea area) {
//        return new Result(service.updateByPrimaryKeySelective(area));
//    }

    @RequestMapping(value = "doUpdate", method = RequestMethod.POST)
    public Result doUpdate(@RequestBody Map<String,Object> map) throws JsonProcessingException {
        return new Result(service.updateByPrimaryKeySelective(map));
    }




    @RequestMapping("toSelect")
    public ModelAndView toSelect(){
        return new ModelAndView("/area/select.html");
    }

  @RequestMapping("toModules")
    public ModelAndView toModules(){
        return new ModelAndView("/modules/font-awesome.html");
    }

    @RequestMapping("index")
    public Result listAll() {
        SysArea sysArea = new SysArea();
        sysArea.setDelFlag("0");
        return new Result(service.select(sysArea));
    }


    @RequestMapping("selectPage/{pageNum}/{pageSize}")
    public Result selectPage(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize")
            int pageSize, @RequestParam Map<String,Object> map) {
        return new Result(service.selectPage(pageNum, pageSize,map));
    }

    @RequestMapping("selectAll")
    public Result selectAll(){
        SysArea sysArea = new SysArea();
        sysArea.setDelFlag("0");
        List<SysArea> sysAreas = service.select(sysArea);
        return new Result(sysAreas);
    }



    //添加
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public Result insert(@RequestBody SysArea area) {
        area.setCreateDate(new Date());
        area.setUpdateDate(new Date());
        area.setDelFlag("0");
        return new Result(service.insertSelective(area));
    }

    @RequestMapping(value = "/toDemand", method = RequestMethod.GET)
    public ModelAndView toDemand() {
        return new ModelAndView("/area/detail.html");
    }



    //文件上传
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public Result upload(MultipartFile file) throws IOException {
        service.upload(file.getInputStream());
        return new Result("生成成功");
    }


    //文件下载
    @RequestMapping(value = "download", method = RequestMethod.GET)
    public void download(HttpServletResponse response) throws IOException {
       response.setHeader("content-disposition","attachment;filename="
               +new String("区域管理.xlsx".getBytes(),"iso8859-1"));
       service.download(response.getOutputStream());
    }




}
